﻿unit Tablefm;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  System.UIConsts,System.Math,System.Generics.Collections,FMX.Objects,FMX.Types,
  FMX.Controls, FMX.Forms, FMX.Dialogs,
  FMX.StdCtrls, FMX.TabControl,UModel,UMyShape;

type
  TTableForm = class(TForm)
    TabControl1: TTabControl;
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
    NeedRefresh : boolean;
    TableLayouts : TTablePageList;
    panelList : TList<TPanel>;
    TextFont : TFont;
    procedure CreateTextFont;
    procedure SetPositionAndSize(shape:TShape;TheTable:TTable);
    function  GetTableColor(table : TTable):TAlphaColor;
    procedure RefreshTableStatus;
    Procedure SetShapeOfATable(table: TTable;TabIndex:integer);
    procedure MyShapeClick(Sender: TObject);
    procedure DisplayTablesLayouts;
    procedure AddTablesToControl;
  public
    { Public declarations }
    //SelectTable : TTable;
    SelectTableStatus : integer;
    SelectTableNo : string;
  end;

var
  TableForm: TTableForm;

implementation

{$R *.fmx}
 function TTableForm.GetTableColor(table : TTable):TAlphaColor;
 begin
   if not table.IsTable then
      result := claYellow
   else
     if table.Status = 0 then
        result := claGreen
     else
        result := clared; //claRed;
 end;


procedure TTableForm.FormShow(Sender: TObject);
begin
   if NeedRefresh then
     RefreshTableStatus
   else
     NeedRefresh := true;
end;

procedure TTableForm.RefreshTableStatus;
var i : integer;
begin
  TableLayouts.RefreshTableStatus;
  AddTablesToControl;
end;

procedure TTableForm.FormClose(Sender: TObject; var Action: TCloseAction);
var j,i : integer;
begin
  for j :=panelList.Count - 1 downto 0 do
     for i := panelList[j].ControlsCount - 1  downto 0 do
        panelList[j].Controls[i].Free;
end;

procedure TTableForm.FormCreate(Sender: TObject);
begin
  panelList := TList<TPanel>.Create;
  NeedRefresh := false;
  CreateTextFont;
  TableLayouts := TTablePageList.Create;
  DisplayTablesLayouts;
end;

procedure TTableForm.CreateTextFont;
Begin
   TextFont := TFont.Create;
   TextFont.Size := 18;
   TextFont.Style := [TFontStyle.fsBold];
   TextFont.Family := 'Arial';
end;


procedure TTableForm.AddTablesToControl;
var i,j : integer;
    TheTable : TTable;
begin
  for j := 0  to TabControl1.TabCount -1  do
  begin
     for I := 0 to TableLayouts.TablePageList[j].Tables.Count - 1 do
     begin
       TheTable := TableLayouts.TablePageList[j].Tables[i];
       SetShapeOfATable(TheTable,j);
     end;
  end;

    TabControl1.ActiveTab := TabControl1.Tabs[0];
 end;

procedure TTableForm.DisplayTablesLayouts;
var
  i: Integer;
  tabitem: TTabItem;
  panel : TPanel;
begin
  //创建楼面 Tabs
  for I := 0 to TableLayouts.TablePageList.Count - 1 do
  begin
     tabitem := TTabItem.Create(TabControl1);
     tabitem.Text := TableLayouts.TablePageList[i].Description;
     tabitem.Tag := TableLayouts.TablePageList[i].PageNo;
     tabitem.Parent := TabControl1;
     panel := TPanel.Create(self);
     panel.Parent := tabItem;
     panel.Align := TAlignLayOut.alClient;
     panelList.Add(panel);
  end;
  AddTablesToControl;
end;



procedure TTableForm.MyShapeClick(Sender: TObject);
begin
   if Sender is TMyEllipse then
   begin
     SelectTableStatus := (Sender as TMyEllipse).Tag;
     SelectTableNo := (Sender as TMyEllipse).TagString;
   end
   else
   begin
      SelectTableStatus := (Sender as TMyRectAngle).Tag;
      SelectTableNo := (Sender as TMyRectAngle).TagString;
   end;
   self.ModalResult := mrOK;    // Notify the Orderfm a table has been selected
   self.CloseModal;
end;

procedure TTableForm.SetPositionAndSize(shape:TShape;TheTable:TTable);
begin
   shape.Position.X := TheTable.X;
   shape.Position.Y := TheTable.Y;
   shape.Width := TheTable.Width;
   shape.Height := TheTable.Height;

   if TheTable.IsTable then             // Add click handler to table shape
      shape.OnClick := self.MyShapeClick;
   shape.Tag := TheTable.Status;        // Stored the table's Status to shape
   shape.TagString := TheTable.TableNo; // Stored the table's TableNo to shape
End;

procedure TTableForm.SetShapeOfATable(table: TTable;TabIndex:integer);
var
  ellipse : TMyEllipse;
  rectangle : TMyRectAngle;
begin
   case table.Shape of
     0:                                 //circle
      begin
         ellipse := TMyEllipse.Create(panelList[TabIndex]);
         SetPositionAndSize(ellipse,table);
         ellipse.Fill.Color := self.GetTableColor(table);
         ellipse.Text := table.Description;
         ellipse.Font := TextFont;
         ellipse.TextColor := claWhite;
         //ellipse.StrokeThickness :=0; Can't be run under iOS, it may be a bug in rad studio
         //ellipse.TagObject := table;
         //ellipse.TagString := table.TableNo;
         ellipse.Parent := panelList[TabIndex];
      end;                              // rectangle
      else
      begin
        rectangle := TMyRectangle.Create(panelList[TabIndex]);
        SetPositionAndSize(rectangle,table);
        rectangle.Fill.Color := self.GetTableColor(table);
        rectangle.Text := table.Description;
        rectangle.Font := TextFont;
        //rectangle.TagObject := table;
        //rectangle.TagString :=table.TableNo;
        if Table.IsTable  then
        begin
           rectangle.TextColor := claWhite;
           //rectangle.StrokeThickness :=0; Can't be run under iOS, it may be a bug in rad studio
        end
        else
           rectangle.TextColor := claBlack;
         rectangle.Parent := panelList[TabIndex];
      end;
   end;
end;


end.
